package ingresso

import org.springframework.dao.DataIntegrityViolationException

class IngressoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [ingressoInstanceList: Ingresso.list(params), ingressoInstanceTotal: Ingresso.count()]
    }

	def create(Integer id) {
		
		if(id == null){
			[ingressoInstance: new Ingresso(params)]
		}
		else
		{	
			def ingressoInstance = new Ingresso(params)
			def evento = Evento.get(id)
			ingressoInstance.evento = evento
			
			render(view: "create", model: [ingressoInstance: ingressoInstance])
			return
		}
	}

    def save() {
        def ingressoInstance = new Ingresso(params)
		ingressoInstance.usuario = session.usuario
		
        if (!ingressoInstance.save(flush: true)) {
            render(view: "create", model: [ingressoInstance: ingressoInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'ingresso.label', default: 'Ingresso'), ingressoInstance.id])
        redirect(action: "show", id: ingressoInstance.id)
    }

    def show(Long id) {
        def ingressoInstance = Ingresso.get(id)
        if (!ingressoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'ingresso.label', default: 'Ingresso'), id])
            redirect(action: "list")
            return
        }

        [ingressoInstance: ingressoInstance]
    }

    def edit(Long id) {
        def ingressoInstance = Ingresso.get(id)
        if (!ingressoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'ingresso.label', default: 'Ingresso'), id])
            redirect(action: "list")
            return
        }

        [ingressoInstance: ingressoInstance]
    }

    def update(Long id, Long version) {
        def ingressoInstance = Ingresso.get(id)
        if (!ingressoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'ingresso.label', default: 'Ingresso'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (ingressoInstance.version > version) {
                ingressoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'ingresso.label', default: 'Ingresso')] as Object[],
                          "Another user has updated this Ingresso while you were editing")
                render(view: "edit", model: [ingressoInstance: ingressoInstance])
                return
            }
        }

        ingressoInstance.properties = params

        if (!ingressoInstance.save(flush: true)) {
            render(view: "edit", model: [ingressoInstance: ingressoInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'ingresso.label', default: 'Ingresso'), ingressoInstance.id])
        redirect(action: "show", id: ingressoInstance.id)
    }

    def delete(Long id) {
        def ingressoInstance = Ingresso.get(id)
        if (!ingressoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'ingresso.label', default: 'Ingresso'), id])
            redirect(action: "list")
            return
        }

        try {
            ingressoInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'ingresso.label', default: 'Ingresso'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'ingresso.label', default: 'Ingresso'), id])
            redirect(action: "show", id: id)
        }
    }
}
